<?php
/*------------------------------------------------------------------------
# mod_vportfolio - VPortfolio Module
# ------------------------------------------------------------------------
# author    Vsmart Extensions
# copyright Copyright (C) 2010 www.vsmart-extensions.com. All Rights Reserved.
# @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
# Websites: http://www.vsmart-extensions.com
# Technical Support:  Forum - http://www.vsmart-extensions.com
-------------------------------------------------------------------------*/
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

class modVstPortfolioHelper
{	
	function getImages(&$params)
	{		
		//Get a database object
		$db	=& JFactory::getDBO();
		$category = $params->get( 'category', '0');
		$orderDirection = $params->get( 'order-direction');
		$order='';
		if ($orderDirection == 0)
		{
			$order = ' ORDER BY s.id ASC';
		}
		else if ($orderDirection == 1)
		{
			$order = ' ORDER BY s.id DESC';
		}
		else
		{
			$order = ' ORDER BY r ';
		}
		if ($category != '0')
		{
			$category = '&& c.id IN ( ' .$category. ' )';			
		}
		else
		{
			$category = '';
		}
		
		$query = 	'SELECT i.imageName AS name, s.id AS id,s.title AS title, s.url AS url, RAND() as r  '. 
					'FROM #__vportfolio_image AS i '.
					'LEFT JOIN #__vportfolio AS s ON i.itemId = s.id '.
					'LEFT JOIN #__vportfolio_category AS c ON s.categoryId = c.id '.
					'WHERE s.public = 1 AND i.default = 1 '.$category.$order;
					
		$number_item = (int)$params->get( 'number_item' );
		if ( $number_item > 0 )
		{
			$db->setQuery($query,0,$number_item);
		}	
		else 
		{
				$db->setQuery($query);
		}
		if (!($rows = $db->loadObjectList())) {
			echo "MD ".$db->stderr();
			return;
		}
		return $rows;
	}
	
	function getItemID()
	{		
		//Get a database object
		$db	=& JFactory::getDBO();		
		$query = 	'SELECT m.id '.
					'FROM jos_components AS c '.
					'LEFT JOIN #__menu AS m ON c.id = m.componentid '.
					'WHERE c.name = "V-Portfolio" ' ;		
		$db->setQuery($query);
		$itemID = $db->loadResult();
		return $itemID;
	}
	
	function setImages($rows, &$params)
	{	
		$width = (int)$params->get( 'width' , 170);		
		$height = (int)$params->get( 'height' , 170);	
		$rootpath = dirname(dirname(dirname(__FILE__))).DS;
		$com_image = $rootpath.'components'.DS.'com_vportfolio'.DS.'images'.DS;		
		$mod_image = $rootpath.'modules'.DS.'mod_vportfolio'.DS.'images'.DS;		
		require_once ('helper_image.php');
		$image_helper = & ImageHelper::getInstance();		
		for ($i = 0; $i < $n = count($rows); $i ++) 
		{
			if ($rows[$i]->name != '')
			{	
				$portfolio_title = str_replace(' ', '_',$rows[$i]->title);
				$imagePath = $mod_image.$rows[$i]->id.'_'.$portfolio_title.'_'.$rows[$i]->name;
				if(!file_exists($imagePath) ||  !$image_helper->test_W_H_Image($imagePath, $width, $height)) 
				{	
					$image_helper->deleteImage($imagePath);
					$imageSource = $com_image.$rows[$i]->id.DS.$rows[$i]->name;
					$ext = $image_helper->getTypeIcon($rows[$i]->name);
					$image_helper->create_crop($imageSource, $imagePath, $ext, $width, $height);
				}	
			}
		}
	}
	/*
	@Get list image for each item (id) object
	*/
	function getListImages( $id )
	{
		$db 		=& JFactory::getDBO();
		
		$query = 'SELECT * FROM #__vportfolio_image WHERE itemId="'.$id.'" ORDER BY `default` DESC, id ASC ';
		$db->setQuery($query);
		$images = $db->loadObjectList();
		if(count($images) > 0)
			return $images;
		else
			return 0;
	}
	/*
	@Get config click to thumb
	*/
	function getConfigClickToThumbImage()
	{
		//Click to thumb image
		$db 		=& JFactory::getDBO();		
		$query = 'SELECT `value`  FROM #__vportfolio_config WHERE name="Click to thumb image"';
		$db->setQuery($query);
		$valueConfig = $db->loadResult();		
		return $valueConfig;
	}
	
}

